import { Component, OnInit } from '@angular/core';
import {Course} from "../Course";
import {StudentService} from "../student.service";
import {NzTableSortFn, NzTableSortOrder} from "ng-zorro-antd";


@Component({
  selector: 'app-courses',
  templateUrl: './courses.component.html',
  styleUrls: ['./courses.component.css']
})
export class CoursesComponent implements OnInit {
  courses: Array<Course> = [];

  // 名称筛选查询
  DisplayCourses = [...this.courses];
  searchValue = '';
  visible = false;

  // 学分排序
  listOfCredit =
    {
      title: '课程学分',
      compare: (a: Course, b: Course) => a.credit - b.credit};

  // 必选修筛选
  listOftype = {
    name: '课程性质',
    filterMultiple: true,
    listOfFilter: [
      { text: '必修课', value: '必修课' },
      { text: '选修课', value: '选修课', byDefault: true }],
    filterFn: (list: string[], item: Course) => list.some(name => item.type.indexOf(name) !== -1)};

  constructor(
    private studentService: StudentService,
  ) { }

  ngOnInit(): void {
    this.studentService.getCourses().subscribe((data: any) => {
      console.log('getCourses', data)
      if (data.status === 1) {
        this.courses = data.data;
      }
    });
  }

  reset(): void {
    this.searchValue = '';
    this.search();
  }

  search(): void {
    this.visible = false;
    this.DisplayCourses = this.courses.filter((item: Course) => item.coursename.indexOf(this.searchValue) !== -1);
  }

}
